﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BOL.Ventas;
using BOL.Comun;
using BOL;
using System.Data.SqlClient;
using Website.Logger;
using DAL;
using BOL.Compras;
using UtilityExtensions;
using System.Drawing;
using System.Web.Script.Services;
using System.Web.Services;

namespace UILGestion.ObraAdmin.OrdenCompras
{
    public partial class OrdenCompraDetalleForm : System.Web.UI.Page
    {

     

        public OrdenCompraDetalleForm()
        {
            bol= new GetOrdenCompraItemExcel();
        }
        private GetOrdenCompraItemExcel bol;


        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
               // clienteTXT.ChkVal= (string)Request.QueryString["idProyecto"];

                if (!this.IsPostBack)
                {
                    idObra.Value = (string)Request.QueryString["idObra"];
                    
                    //paginaAnt.Value = Page.Request.UrlReferrer.AbsoluteUri;
                    menuCtrl1.loadMenu(int.Parse(idObra.Value), Request.AppRelativeCurrentExecutionFilePath);
                    FormatForm();
                }
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);

                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                     "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }
        private void FormatForm()
        {
            var f = new ListValueBL().GetInsumoClases().ToList();
            ListValueInfo todos= new ListValueInfo();
            todos.descripcion="-----TODOS--------";
            todos.idInternal=0;
            f.Insert(0, todos);


            claseDDL.DataValueField = "idInternal";
            claseDDL.DataTextField = "descripcion";
            claseDDL.DataSource = f;
            claseDDL.DataBind();

            var r = new ListValueBL().GetInsumoFamiliaXClase(claseDDL.SelectedValue.ToInt());
            r.Insert(0, todos);

            familiaDDL.DataValueField = "idInternal";
            familiaDDL.DataTextField = "descripcion";
            familiaDDL.DataSource = r;
            familiaDDL.DataBind();


            var s = new ListValueBL().GetInsumoSubFamiliaXFamilia(familiaDDL.SelectedValue.ToInt());
            s.Insert(0, todos);

          

            
            subfamiliaDDL.DataValueField = "idInternal";
            subfamiliaDDL.DataTextField = "descripcion";
            subfamiliaDDL.DataSource = s;
            subfamiliaDDL.DataBind();


            var t = new ListValueBL().GetMeses().ToList();
            t.Insert(0, todos);


            mesDDL.DataValueField = "idInternal";
            mesDDL.DataTextField = "descripcion";
            mesDDL.DataSource = t;
            mesDDL.DataBind();


            anioDDL.DataValueField = "idInternal";
            anioDDL.DataTextField = "descripcion";
            anioDDL.DataSource = new ListValueBL().GetAniosOrdenCompra(idObra.Value.ToInt());
            anioDDL.DataBind();


            anioDDL.SelectedValue = DateTime.Now.Year.ToString();
            mesDDL.SelectedValue = DateTime.Now.Month.ToString();
            dataBindItem();

            
        }

        private void loadFamilia()

        {
            ListValueInfo todos = new ListValueInfo();
            todos.descripcion = "-----TODOS--------";
            todos.idInternal = 0;
            
            
            var s = new ListValueBL().GetInsumoFamiliaXClase(claseDDL.SelectedValue.ToInt()).ToList();
            s.Insert(0, todos);

            familiaDDL.DataSource = s;
            familiaDDL.DataBind();
            localizacionUPNL.Update();

        }

        private void loadSubFamilia()
        {
            ListValueInfo todos = new ListValueInfo();
            todos.descripcion = "-----TODOS--------";
            todos.idInternal = 0;


            var s = new ListValueBL().GetInsumoSubFamiliaXFamilia(subfamiliaDDL.SelectedValue.ToInt()).ToList();
            s.Insert(0, todos);

            familiaDDL.DataSource = s;
            familiaDDL.DataBind();
            

        }



        private void dataBindItem()
        {
            itemGridView.DataSource =
                bol.Execute(idObra.Value.ToInt(),claseDDL.SelectedValue.ToInt(),
                                                 familiaDDL.SelectedValue.ToInt(),
                                                 subfamiliaDDL.SelectedValue.ToInt(),
                                                                anioDDL.SelectedValue.ToInt(),
                                                                mesDDL.SelectedValue.ToInt());

            itemGridView.DataBind();

        }

        protected void conceptoDDL_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                //dataBindItem();
                loadSubFamilia();
                
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);

                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                     "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }
            
        }

        protected void etapaDDL_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
            loadFamilia();
            loadSubFamilia();
          
            }
             catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);

                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                     "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }
            
        }


        protected void itemGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
        {
            //e.Row.Cells[0].BackColor = Color.FromName("#99CCFF");

            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                var item = (OrdenVentaDetalleInfo)(e.Row.DataItem);
                bool esTotal = item.esTotal;


                if (esTotal)
                {
                    e.Row.BackColor = Color.LightYellow;
                    e.Row.Font.Bold = true;
                }
                
                e.Row.Cells[9].ForeColor = (item.saldo > 0) ? Color.Red : Color.Green;
                //e.Row.Cells[8].ForeColor = (item.hasta3060 > 0) ? Color.Red : Color.Green;
                //e.Row.Cells[9].ForeColor = (item.hasta60mas > 0) ? Color.Red : Color.Green;


            }
        }

        

        protected void Button2_Click(object sender, EventArgs e)
        {
            dataBindItem();
        }

        

       
    }

}